import { useRequest } from 'vue-request';
import { getArea, getIndustry, getTagType, SubjectType } from '@/apis';
import { ComponentType, type MenuProps } from 'vc';
import { ls } from '@/utils';
import { AREA_DATA, INDUSTRY_DATA } from '@/constants';

/**
 * 获取地区 缓存本地
 */
export async function areaData() {
  let area: any = [];
  if (ls.get(AREA_DATA)) {
    area = ls.get(AREA_DATA) || '[]';
  } else {
    await getArea().then((res) => {
      area = res;
      ls.set(AREA_DATA, area);
    });
  }
  return area;
}

/**
 * 获取行业 缓存本地
 */
export async function industryData() {
  let industry: any = [];
  if (ls.get(INDUSTRY_DATA)) {
    industry = ls.get(INDUSTRY_DATA) || [];
  } else {
    await getIndustry().then((res) => {
      industry = res;
      ls.set(INDUSTRY_DATA, industry);
    });
  }
  return industry;
}

/**
 * @description 地区 对应的配置
 */
export function getAreaConfig() {
  const { data } = useRequest(areaData);

  return computed(() => {
    return {
      title: '地区',
      type: 'area',
      component: ComponentType.CASCADER,
      options: data,
      submitFn: (val) => {}
    };
  });
}

/**
 * @description 行业 对应的配置
 */
export function getIndustryConfig() {
  const { data } = useRequest(industryData);

  return computed(() => {
    return {
      title: '行业',
      component: ComponentType.CASCADER,
      submitFn: (val) => {},
      options: data
    };
  });
}

/**
 * @description 企业类型 对应的配置
 */
export function getTagTypeConfig(defaultParams: SubjectType) {
  const { data } = useRequest(getTagType, {
    defaultParams: [defaultParams]
  });

  return computed(() => {
    return {
      title: '企业类型',
      component: ComponentType.MULTIPLE,
      submitFn: (val) => {},
      options: data || []
    };
  });
}
